package org.example.lethear.repository;

import org.example.lethear.entity.WhisperSettings;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 悄悄话设置数据访问接口
 */
@Repository
public interface WhisperSettingsRepository extends JpaRepository<WhisperSettings, Long> {
    
    /**
     * 根据用户ID查找设置
     */
    Optional<WhisperSettings> findByUserId(Long userId);
    
    /**
     * 检查用户是否已有设置
     */
    boolean existsByUserId(Long userId);
    
    /**
     * 查找允许匿名留言的用户设置
     */
    @Query("SELECT ws FROM WhisperSettings ws WHERE ws.allowAnonymous = 1")
    List<WhisperSettings> findUsersAllowingAnonymous();
    
    /**
     * 查找需要审核的用户设置
     */
    @Query("SELECT ws FROM WhisperSettings ws WHERE ws.needReview = 1")
    List<WhisperSettings> findUsersRequiringReview();
    
    /**
     * 根据每日最大留言数查找设置
     */
    @Query("SELECT ws FROM WhisperSettings ws WHERE ws.maxDailyCount >= :minCount")
    List<WhisperSettings> findByMaxDailyCountGreaterThanEqual(@Param("minCount") Integer minCount);
    
    /**
     * 统计允许匿名留言的用户数
     */
    @Query("SELECT COUNT(ws) FROM WhisperSettings ws WHERE ws.allowAnonymous = 1")
    long countUsersAllowingAnonymous();
    
    /**
     * 统计需要审核的用户数
     */
    @Query("SELECT COUNT(ws) FROM WhisperSettings ws WHERE ws.needReview = 1")
    long countUsersRequiringReview();
} 